#encoding=utf8
import re,json
#百词斩词库
doneList = json.load(open("dict/doneList.json"))
passedList = json.load(open("dict/passedList.json"))
#自定义词 停用词
otherKnowWord=set(open("list/otherKnowWord.txt").read().split("\n"))
#四级 初中
cet4Word=eval((open("dict/cet4Word.json").read()))
seniorWord=eval((open("dict/seniorWord.json").read()))
juniorWord=eval((open("dict/juniorWord.json").read()))
#英汉字典
en2Zhdict=eval((open("dict/en2Zhdict.json").read()))
#学过的词汇
#待转换的文章

fileName = "in.txt"
title=open(fileName).read()


#得到单词的词根
def getStemmer(word):
    words = [word]
    if word.lower() != word : words.append(word.lower())
    word=word.lower()
    s3=word[-3:]
    s2=word[-2:]
    s1=word[-1:]
    h3=word[:-3]
    h2=word[:-2]
    h1=word[:-1]
    if s3 == "ing":
        words.append(h3)
        words.append(h3+"i")
        words.append(h3+"in")
        words.append(h3+"e")
        if len(h3)>3 and h3[-1]==h3[-2] :words.append(h3[0:-1])
    if s2 == "ed":
        words.append(h2)
        words.append(h2+"e")
        if len(h2)>3 and h2[-1]==h2[-2] :words.append(h2[0:-1])
    if s2 == "es":
        words.append(h2+"e")
    if s3 == "ies":
        words.append(h3+"y")
    if s1 == "s":
        words.append(h1)
    if s2 == "er":
        words.append(h2)
        words.append(h2+'e')
    if s3 == "ied":
        words.append(h3+"y")
    if s2 == "ly":
        words.append(h2)
        words.append(h2+"l")
    return words
    # print words

# getStemmer("doing")    
# getStemmer("called")    
# getStemmer("apples")    
# getStemmer("teacher")    
# getStemmer("studied")    
# getStemmer("apply")    
# print getStemmer("Updated")
# print getStemmer("getting")

unKnowWord=[]
def subFun(t):
    def DictUnion(l,d):
        for i in l:
            if d.has_key(i): 
                return (True,i)
        return False

    word = t.group()
    words=getStemmer(word)
    #数字
    if word.isdigit() : return "<b>%s</b>"%word
    #停用词,高中词汇和学过的词汇不显示释义,四级词汇显示
    if set(words)&otherKnowWord : return "<b>%s</b>"%word
    if DictUnion(words,doneList) : return "<b>%s</b>"%word
    if DictUnion(words,passedList) : return "<b>%s</b>"%word
    if DictUnion(words,juniorWord) : return "<b>%s</b>"%word
    if DictUnion(words,seniorWord) : return "<b>%s</b>"%word

    r = DictUnion(words,cet4Word)
    if r :
        unKnowWord.append((word,cet4Word[r[1]]))
        if r[1]==word:
            return "<b>%s</b><sub>(%s)</sub>"%(word,cet4Word[r[1]])
        else :
            return "<b>%s</b><sub>(%s:%s)</sub>"%(word,r[1],cet4Word[r[1]])
    
    r = DictUnion(words,en2Zhdict)
    if r == False: 
        # print word
        return "<i>%s</i>"%word
    unKnowWord.append((word,en2Zhdict[r[1]]))
    if r[1]==word:
        return "<b>%s</b><sub>(%s)</sub>"%(word,en2Zhdict[r[1]])
    else :
        return "<b>%s</b><sub>(%s:%s)</sub>"%(word,r[1],en2Zhdict[r[1]])




output = re.sub(r"\w+",subFun,title)
output = output.replace("\n","<br />")


# #翻译
# import googleTranslate
# unKnowWord=list(set(unKnowWord))
# translator= googleTranslate.Translator(from_lang="en", to_lang="zh")
# translation = translator.translate("\n\n".join(unKnowWord))
# # print translation

# unKnowWordAns = translation.split('\n\n')
# # print unKnowWordAns
# # def pr(t):
#     # print json.dumps(t, ensure_ascii=False,indent=2)

# # print json.dumps(zip(unKnowWord,unKnowWordAns))

# for i,j in enumerate(unKnowWord):
#     # print i,j,unKnowWordAns[i]
#     if unKnowWordAns[i].find(j)==-1:
#         output = output.replace(j,"%s<sub>%s</sub>"%(j,unKnowWordAns[i].encode("utf8")))
# # print output

#css
output = '''
<meta charset="UTF-8">
<style type="text/css">
body{
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 16px;
}
b{
    padding: 2px 4px;
    font-size: 120%;
    color: #c7254e;
    white-space: nowrap;
    /* background-color: #f9f2f4; */
    border-radius: 4px;
}
sub{
    font-size: 80%;
}
</style>
'''+output


open("out.html","w").write(output)

# print unKnowWord

unKnowWord=list(set(unKnowWord))
unKnowWord.sort()
unKnowWordText=""
for i in unKnowWord:unKnowWordText+=(i[0]+":"+i[1]+'\n')

open("word.txt","w").write(unKnowWordText)
